package com.yixing.demo.controller;

import com.yixing.demo.component.JwtClass;
import com.yixing.demo.constants.Constants;
import com.yixing.demo.model.User;
import com.yixing.demo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;
    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @RequestMapping("/login")
    public Object login(String username, String password, HttpSession session) {
        log.info(username);
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return new User();
        }
        User userInfo = userService.login(username);
        if(userInfo == null) {
            return new User();
        }else {
            if(bCryptPasswordEncoder.matches(password, userInfo.getPassword())) {
                //密码正确, 生成token
                Map<String,Object> claim = new HashMap<>();
                claim.put(Constants.USER_ID, userInfo.getUserId());
                claim.put(Constants.USER_NAME, userInfo.getUsername());
                userInfo.setToken(JwtClass.genToken(claim));
                userInfo.setPassword("");
                session.setAttribute(Constants.USER_SESSION, userInfo);
                return userInfo;
            }else {
                return new User();
            }
        }
    }

    @RequestMapping("/register")
    public Object register(User user) {
        String encoderPassword = bCryptPasswordEncoder.encode(user.getPassword());
        user.setPassword(encoderPassword);
        try{
            userService.register(user);
        } catch (DuplicateKeyException e) {
            log.error("用户注册失败");
            return new User();
        }
        user.setPassword("");
        return user;
    }


    @RequestMapping("/userInfo")
    public Object getUserInfo(HttpServletRequest req) {
        HttpSession session = req.getSession(false);
        if (session == null) {
            System.out.println("[getUserInfo] 当前获取不到 session 对象!");
            return new User();
        }
        User user = (User) session.getAttribute(Constants.USER_SESSION);
        if (user == null) {
            System.out.println("[getUserInfo] 当前获取不到 user 对象!");
            return new User();
        }
        user.setPassword("");
        return user;
    }

//    @RequestMapping("/uploadavatar")
//    public Object uploadavatar(HttpServletRequest req) {
//        HttpSession session = req.getSession(false);
//        if (session == null) {
//            System.out.println("[getUserInfo] 当前获取不到 session 对象!");
//            return new User();
//        }
//        User user = (User) session.getAttribute(Constants.USER_SESSION);
//        if (user == null) {
//            System.out.println("[getUserInfo] 当前获取不到 user 对象!");
//            return new User();
//        }
//        user.setPassword("");
//        return user;
//    }
}
